<template>
  <div>
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <el-table :data='roleList'
                border
                stripe>
        <!-- 权限列表 -->
        <el-table-column type='expand'>
          <template slot-scope="scope">
            <el-row v-for="(item1,i1) in scope.row.children"
                    :key='item1.id'
                    :class="['tagbottom',i1===0?'tagtop':'','vcenter']">
              <!-- 第一级权限 -->
              <el-col :span="6">
                <el-tag closable
                        @close="deleteRight(scope.row,item3.id)">{{item1.authName}}</el-tag>
              </el-col>
              <!-- 第二级权限，第三级权限 -->
              <el-col :span="18">
                <el-row v-for="(item2,i2) in item1.children"
                        :key='item2.id'
                        :class="[i2===0?'':'tagtop','vcenter']">
                  <el-col :span="6">
                    <el-tag type="success"
                            closable
                            @close="deleteRight(scope.row,item2.id)">{{item2.authName}}</el-tag>
                  </el-col>
                  <el-col :span="18">
                    <el-tag type="warning"
                            v-for="(item3) in item2.children"
                            :key='item3.id'
                            closable
                            @close="deleteRight(scope.row,item3.id)">{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type='index'></el-table-column>
        <el-table-column prop='roleName'
                         label="角色名称"></el-table-column>
        <el-table-column prop='roleDesc'
                         label="角色描述"></el-table-column>
        <el-table-column label="操作"
                         width="500px">
          <template slot-scope="scope">
            <!-- 修改角色权限按钮 -->
            <el-button type="primary"
                       icon="el-icon-edit"
                       size="mini">修改角色权限</el-button>
            <!-- 删除角色权限按钮 -->
            <el-button type="danger"
                       icon="el-icon-delete"
                       size="mini">角色删除</el-button>
            <!-- 分配角色权限按钮 -->
            <el-tooltip class="item"
                        effect="dark"
                        content="分配权限"
                        placement="top"
                        :enterable='false'>
              <el-button type="warning"
                         icon="el-icon-setting"
                         size="mini"
                         @click="showRightsDialog(scope.row)">分配角色权限</el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 权限列表对话框 -->
    <el-dialog title="权限列表"
               :visible.sync="setRightDialogVisible"
               width="50%"
               @close="resetRightsDialog">
      <el-tree :data="rightsList"
               :props="defaultProps"
               show-checkbox
               node-key="id"
               :default-expand-all='true'
               :default-checked-keys="nodearr"
               ref="treeRef"></el-tree>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="setRightDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="confirmRightset">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      roleList: [],
      setRightDialogVisible: false,
      rightsList: [],
      defaultProps: {
        label: 'authName',
        children: 'children'
      },
      nodearr: [],
      roleid: ''
    }
  },
  created () {
    this.getRolelist()
  },
  methods: {
    // 点击确认分配权限
    confirmRightset: function () {
      const roleidarr = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]
      const rloeidstr = roleidarr.join(',')
      const that = this
      this.$http.post(`roles/${this.roleid}/rights`, { rids: rloeidstr }).then(function (res) {
        console.log(res)
        if (res.data.meta.status !== 200) {
          that.$message.error('添加用户权限失败')
        }
        that.$message.success('添加用户权限成功')
        that.getRolelist()
      }, function (err) {
        console.log(err)
      })
      this.setRightDialogVisible = false
    },
    getRolelist: function () {
      const that = this
      this.$http.get('roles').then(function (res) {
        that.roleList = res.data.data
      }, function (err) {
        console.log(err)
      })
    },
    // 删除权限
    deleteRight: function (roles, rightid) {
      this.$confirm('你将永久删除用户该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const that = this
        this.$http.delete(`roles/${roles.id}/rights/${rightid}`).then(function (response) {
          if (response.data.meta.status !== 200) {
            return that.$message.error('删除用户失败')
          } else {
            roles.children = response.data.data
            that.$message({
              type: 'success',
              message: '删除成功!'
            })
          }
        }, function (err) {
          console.log(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 分配权限列表
    showRightsDialog: function (role) {
      const that = this
      this.roleid = role.id
      this.$http.get('rights/tree').then(function (res) {
        that.rightsList = res.data.data
      }, function (err) {
        console.log(err)
      })
      this.checkRights(role, this.nodearr)
      this.setRightDialogVisible = true
    },
    // 勾选已有权限
    checkRights (node, nodearr) {
      if (!node.children) {
        return nodearr.push(node.id)
      }
      node.children.forEach(item => {
        this.checkRights(item, nodearr)
      })
    },
    resetRightsDialog: function () {
      this.nodearr = []
    }
  }
}
</script>
<style lang="less" scoped>
.el-row {
  margin-bottom: 10px;
}
.el-card {
  line-height: 0;
}
.tagtop {
  border-top: 1px solid #eee;
}
.tagbottom {
  border-bottom: 1px solid #eee;
}
.el-tag {
  margin: 10px;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>
